package com.thinkgem.jeesite.common.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class MyJPush {

    
    
//    private static final Boolean flag = true;//生产
    	private static final Boolean flag = false;//开发
    
	protected static final Logger LOG = LoggerFactory.getLogger(MyJPush.class);
	
	
	private static final String appKey = "20c092fdc212df7899b25486";
	private static final String masterSecret = "984e613e12a59eec87afc48d";
	
	

	
	/**
	 * 向所有人推送
	 * 
	 * @param title
	 * @param content
	 */
	public static void pushAll(String title, Map<String, String> content) {
	    	JPushClient jpushClient;	
	    	jpushClient = new JPushClient(masterSecret, appKey, 5);

		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_all_all_alert(title, content);

		try {
			PushResult result = jpushClient.sendPush(payload);
//			System.out.println(result);

		} catch (APIConnectionException e) {
			// Connection error, should retry later
			System.err.println("Connection error, should retry later");
			e.printStackTrace();
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			System.err.println("Should review the error, and fix the request"
					+ e);
			System.err.println("HTTP Status: " + e.getStatus());
			System.err.println("Error Code: " + e.getErrorCode());
			System.err.println("Error Message: " + e.getErrorMessage());
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 推送一个tag
	 * @param title
	 * @param content
	 * @param tag
	 */
	public static void pushByTag(String title, Map<String, String> content,String tag) {
	    	JPushClient jpushClient;	
	    	jpushClient = new JPushClient(masterSecret, appKey, 5);

		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_tag_notification(title, content, tag);

		try {
			PushResult result = jpushClient.sendPush(payload);
//			System.out.println(result);

		} catch (APIConnectionException e) {
			// Connection error, should retry later
			System.err.println("Connection error, should retry later");

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			System.err.println("Should review the error, and fix the request"
					+ e);
			System.err.println("HTTP Status: " + e.getStatus());
			System.err.println("Error Code: " + e.getErrorCode());
			System.err.println("Error Message: " + e.getErrorMessage());
		}
	}

	
	/**
	 * 多个别名，相同内容。Android ios发送 notification
	 * 
	 * @param aliases
	 * @param title
	 * @param content
	 * @param type
	 */
	public static void pushAliases(List<String> aliases, String title,
			Map<String, String> content) {
	    	JPushClient jpushClient;	
	    	jpushClient = new JPushClient(masterSecret, appKey, 5);
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_all_alias_messageandnotification(
				aliases, title, content);

		try {
			PushResult result = jpushClient.sendPush(payload);
//			System.out.println(result);

		} catch (APIConnectionException e) {
			// Connection error, should retry later
			System.err.println("Connection error, should retry later");

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			System.err.println("Should review the error, and fix the request"
					+ e);
			System.err.println("HTTP Status: " + e.getStatus());
			System.err.println("Error Code: " + e.getErrorCode());
			System.err.println("Error Message: " + e.getErrorMessage());
		}
	}

	
	/**
	 * 一个别名，相同内容。Android ios发送 notification
	 * 
	 * @param aliases
	 * @param title
	 * @param content
	 * @param type
	 *            0user 1teacher 2admin
	 */
	public static void pushAlias(String alias, String title,
			Map<String, String> content) {
	    	JPushClient jpushClient;	
	    	jpushClient = new JPushClient(masterSecret, appKey, 5);
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_all_alias_messageandnotification(
				alias, title, content);
		//System.out.println("@||          |"+payload);
		try {
			PushResult result = jpushClient.sendPush(payload);
//			System.out.println(result);

		} catch (APIConnectionException e) {
			// Connection error, should retry later
			System.err.println("Connection error, should retry later");

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			System.err.println("Should review the error, and fix the request"
					+ e);
			System.err.println("HTTP Status: " + e.getStatus());
			System.err.println("Error Code: " + e.getErrorCode());
			System.err.println("Error Message: " + e.getErrorMessage());
		}
	}

	private static PushPayload buildPushObject_all_all_alert(String title,
			Map<String, String> content) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.all())
				.setMessage(
						Message.newBuilder()
								.setTitle(title)
								.setMsgContent(
										JSONArray.fromObject(content)
												.toString()).build())
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(title)
												.setSound("happy.caf")
												.addExtras(content).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(title)
//												.setTitle(title)
												.addExtras(content).build())
								.build())
				.setOptions(Options.newBuilder()
                         .setApnsProduction(flag)
                         .build())
                     .build();

	}
	
	private static PushPayload buildPushObject_tag_notification(String title,Map<String,String> content,String tag){
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.tag(tag))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(title)
                                .setSound("happy.caf")
                                .addExtras(content)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                        		.setAlert(title)
//                        		.setTitle(title)
                        		.addExtras(content)
                        		.build())
                			.build())
    			.setOptions(Options.newBuilder()
                         .setApnsProduction(flag)
                         .build())
        			.build();
	}

	private static PushPayload buildPushObject_all_alias_messageandnotification(
			List<String> aliases, String title, Map<String, String> content) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.alias(aliases))
				.setMessage(
						Message.newBuilder()
								.setTitle(title)
								.setMsgContent(
										JSONArray.fromObject(content)
												.toString()).build())
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(title)
												.setSound("happy.caf")
												.addExtras(content).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(title)
//												.setTitle(title)
												.addExtras(content).build())
								.build())
				.setOptions(Options.newBuilder()
                         .setApnsProduction(flag)
                         .build())
                     .build();

	}

	private static PushPayload buildPushObject_all_alias_messageandnotification(
			String alias, String title, Map<String, String> content) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.alias(alias))
				.setMessage(
						Message.newBuilder()
								.setTitle(title)
								.setMsgContent(
										JSONArray.fromObject(content)
												.toString()).build())
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(title)
												.setSound("happy.caf")
												.addExtras(content).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(title)
//												.setTitle(title)
												.addExtras(content).build())
								.build())
				.setOptions(Options.newBuilder()
                         .setApnsProduction(flag)
                         .build())
                     .build();

	}

	public static void main(String[] args) {
		List<String> l = new ArrayList<String>();

		Map<String, String> content = new HashMap<String, String>();
		content.put("messageNoJump", "0");
		content.put("DATE",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
		//pushAlias("37","张仰泽测试",content);
		pushAll("张仰泽",content);
		
	}

}
